package cn.com.greatwall.system.modules.security.security;

import java.util.Collection;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import cn.com.greatwall.common.config.RsaProperties;
import cn.com.greatwall.common.exception.BadRequestException;
import cn.com.greatwall.common.utils.RsaUtils;
import cn.com.greatwall.common.utils.SpringContextHolder;
import cn.com.greatwall.system.modules.security.service.UserDetailsServiceImpl;
import cn.com.greatwall.system.modules.security.service.dto.JwtUserDto;
import cn.com.greatwall.system.modules.system.service.UserService;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author herw
 * @Time 2020-10-26 14:17:49
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
@Slf4j
@Service
public class SecurityAuthenticationProvider implements AuthenticationProvider {
//    @Autowired
//    private UserDetailsServiceImpl userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserService userService;

    @Autowired
    HttpSession httpSession;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String username = authentication.getName();
        String password = (String) authentication.getCredentials();
//        // 密码解密
//        String password = null;
//        try {
//            password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, (String) authentication.getCredentials());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
        JwtUserDto user = (JwtUserDto) userDetailsService.loadUserByUsername(username);
        if (user == null) {
//            throw new BadCredentialsException("用户不存在！");
            throw new BadRequestException("用户不存在！");
        }
        if (!passwordEncoder.matches(password, user.getPassword())) {
//            throw new BadCredentialsException("密码不正确！");
            throw new BadRequestException("密码不正确！");
        }

        Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
        return new UsernamePasswordAuthenticationToken(user, password, authorities);// 构建返回的用户登录成功的token
    }

    /**
     * 执行支持判断
     */
    @Override
    public boolean supports(Class<?> authentication) {
        return true;// 返回 true，表示支持执行
    }

}
